package com.lvyq.qrobot.service.impl;

import com.lvyq.qrobot.common.UrlConstant;
import com.lvyq.qrobot.entity.SysGovAffair;
import com.lvyq.qrobot.entity.SysOrder;
import com.lvyq.qrobot.entity.UserEntity;
import com.lvyq.qrobot.entity.UserGovAffair;
import com.lvyq.qrobot.mapper.SysGovAffairMapper;
import com.lvyq.qrobot.mapper.UserGovAffairMapper;
import com.lvyq.qrobot.mapper.UserMapper;
import com.lvyq.qrobot.model.receive.ReceiveMsg;
import com.lvyq.qrobot.model.send.SendParam;
import com.lvyq.qrobot.service.SysGovAffairService;
import com.lvyq.qrobot.service.UserGovAffairService;
import com.lvyq.qrobot.service.UserService;
import com.lvyq.qrobot.util.DateUtil;
import com.lvyq.qrobot.util.HttpClientUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author lvyq 政务任务信息
 * @version 1.0
 * Create by 2024-06-04 14:20
 */
@Service
public class SysGovAffairServiceImpl implements SysGovAffairService {

    @Resource
    public UserService userService;

    @Resource
    public UserGovAffairService userGovAffairService;

    @Resource
    public UserGovAffairMapper userGovAffairMapper;

    @Resource
    public SysGovAffairMapper govAffairMapper;

    @Resource
    public UserMapper userMapper;
    @Override
    public void sendMsg(SysOrder order, SendParam param, ReceiveMsg vo) {
        if ("政务".equals(vo.message)){
            govAffairInfo(order,param,vo);
        }

        //政务开启
        if (order.secType==1){
            govStandGuard(order,param,vo);
        }
    }

    /**
     * @description
     * @author lvyq
     * @param[1] order
     * @param[2] param
     * @param[3] vo
     * @throws
     * @time 2024-06-04 14:47
     */
    private void govStandGuard(SysOrder order, SendParam param, ReceiveMsg vo) {
        UserEntity user = userService.getUser(vo);
        //查询当前是否有正在进行的政务
        UserGovAffair userGovAffair = userGovAffairService.getInfo(vo);
        String content = order.content.replace("{1}",vo.userId.toString());

        if (user.affairingNum<user.affairNum){
            if (userGovAffair!=null){
                content=content.replace("{2}","["+vo.message+"]失败,你正在进行["+userGovAffair.name+"]");
            }else {

                //查询政务信息
                SysGovAffair govAffairs = govAffairMapper.selectWithLevelAndName(user.positionLevel,vo.message);
                if (govAffairs!=null){
                    if (user.tili<govAffairs.tili){
                        content=content.replace("{2}","["+vo.message+"]失败,你的体力不足]");
                    }else {
                        user.setAffairingNum(user.affairingNum+1);
                        userGovAffair= new UserGovAffair();
                        userGovAffair.setUserId(vo.userId);
                        userGovAffair.setName(vo.message);
                        userGovAffair.setJingyan(govAffairs.jingyan);
                        userGovAffair.setGroupId(vo.groupId);
                        userGovAffair.setTili(govAffairs.tili);
                        long startTime=new Date().getTime();
                        long endTime=startTime+(govAffairs.time*1000*60);
                        userGovAffair.setStart(startTime);
                        userGovAffair.setEnd(endTime);
                        userGovAffairMapper.insert(userGovAffair);
                        userMapper.updateById(user);
                        String govContent = govAffairs.content.replace("{1}", DateUtil.timeChange(userGovAffair.getEnd()));
                        content=content.replace("{2}",govContent);
                    }

                }else {
                    content=content.replace("{2}","["+vo.message+"]失败,请提高官职]");
                }

            }
        }else {
            content=content.replace("{2}","["+vo.message+"]失败,今日政务已达上限");
        }

        param.setMessage(content);
        HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
    }

    /**
     * @description 政务信息查询
     * @author lvyq
     * @param[1] sysOrder
     * @param[2] param
     * @param[3] vo
     * @throws
     * @time 2024-06-04 14:23
     */
    private void govAffairInfo(SysOrder sysOrder, SendParam param, ReceiveMsg vo) {
        UserEntity user = userService.getUser(vo);
        List<SysGovAffair> govAffairs = govAffairMapper.listWithLevel(user.positionLevel);
        StringBuffer buffer = new StringBuffer();
        String content = sysOrder.content.replace("{1}",vo.userId.toString())
                        .replace("{2}",user.position)
                        .replace("{4}",user.affairingNum+"/"+user.affairNum);
        govAffairs.forEach(govAffair -> {
            //查询当前是否有正在进行的政务
            UserGovAffair userGovAffair = userGovAffairService.getInfo(vo);
            if (userGovAffair!=null && govAffair.name.equals(userGovAffair.name)){
                String timeChange = DateUtil.timeChange(userGovAffair.end);
                if (timeChange.equals("")){
                    timeChange="(已完成)";
                }else {
                    timeChange="(剩"+timeChange+")";
                }
                buffer.append("❗️"+govAffair.name+"|"+govAffair.time+"|"+govAffair.jingyan+timeChange+"\n");
            }else {
                buffer.append(govAffair.name+"|"+govAffair.time+"|"+govAffair.jingyan+"\n");
            }

        });
        content=content.replace("{3}",buffer.toString().substring(0,buffer.length()-1));
        param.setMessage(content);
        HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
    }
}
